package frame;

import pojo.UserDO;
import service.UserServise;
import service.impl.UserServiseImpl;
import util.AtmUtil;
import util.GuiImg;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JPanel implements ActionListener {
    private Icon atmIcon1 = GuiImg.getIcon("/imgs/buttonLand.jpg");
    private Icon atmIcon2 = GuiImg.getIcon("/imgs/buttonLogin.jpg");
    private JButton btnlogin = new JButton(atmIcon1);
    private JTextField textField1 = new JTextField();
    private JPasswordField passw = new JPasswordField();
    private Atm frame;
    private int number = 0;

    public Login(Atm frame) {

        this.frame = frame;
        this.setLayout(null);
        //账号标签
        JLabel textlabel1 = new JLabel("用户名：");
        textlabel1.setBounds(200, 150, 50, 30);
        this.add(textlabel1);
        //单行输入框
        textField1.setBounds(250, 150, 250, 30);
        this.add(textField1);
        //密码标签
        JLabel textlabel2 = new JLabel("密码：");
        textlabel2.setBounds(200, 200, 50, 30);
        this.add(textlabel2);
        //密码输入框
        passw.setBounds(250, 200, 250, 30);
        this.add(passw);
        //登录按钮
        this.setOpaque(false);
        btnlogin.setBounds(250, 300, 100, 40);
        this.add(btnlogin);
        btnlogin.addActionListener(this);
        //注册按钮
        JButton btnEnroll = new JButton(atmIcon2);
        btnEnroll.setBounds(400, 300, 100, 40);
        this.add(btnEnroll);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        UserServise userServise=new UserServiseImpl();
        if (e.getSource() == btnlogin) {
//            for (int i = 0; i < frame.getUserArray().size(); i++) {
//                if (frame.getUserArray().get(i).getId().equals(textField1.getText()) && frame.getUserArray().get(i).getPassword().equals(passw.getText())) {
//                    JOptionPane.showMessageDialog(this, "登录成功");
//                    frame.setIndex(i);
//                    number=0;
//                    frame.switchPanel("菜单");
//                    break;
//                }
//                if (i == frame.getUserArray().size() - 1) {
//                    number++;
//                    JOptionPane.showMessageDialog(this, "账号密码错误，还有" + (3 - number) + "次机会");
//                    break;
//                }
//            }
             UserDO user=  userServise.getUser(textField1.getText(),String.valueOf(passw.getPassword()));
             if (user!=null){
                 frame.setLoginuser(user);
                 frame.switchPanel("菜单");
                 return;
             }
            number++;
            if (number == 3) {
                JOptionPane.showMessageDialog(this, "账号冻结");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "账号密码错误，还有" + (3 - number) + "次机会");
        }

    }
}
